package com.sskj.hangqing.presenter;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.http.WebSocketObserver;
import com.sskj.common.util.DisposUtil;
import com.sskj.hangqing.bean.Stock;
import com.sskj.hangqing.http.HttpConfig;
import com.sskj.hangqing.ui.fragment.ChartFragment;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.SocketBean;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.http.CallBackOption;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;


public class ChartFragmentPresenter extends BasePresenter<ChartFragment> {


    private WebSocketObserver stockSocket;
    private Disposable stockDispo;
    private String code;
    private String period;
    private boolean isBibi;

    /**
     * 获取k线数据
     *
     * @param goodsType 分时数据标示
     * @param code      币种
     * @return
     */
    public void getKData(String goodsType, String code, boolean isBibi) {
        httpService.getKData(500, goodsType, code, isBibi)
                .execute(new CallBackOption<HttpData<List<Stock>>>() {
                    @Override
                    public void onError(Response<HttpData<List<Stock>>> response) {
                        super.onError(response);
                        mView.hideKLoading();
                    }
                }.unLoadBind(mView)
                        .execute(httpData -> {
                            mView.hideKLoading();
                            if (httpData.getStatus() == HttpConfig.OK) {

                                mView.setChart(httpData.getData());
                            }
                        }));
    }

    public void initWebSocket(String code, String period, boolean isBibi) {

        if (stockSocket == null) {
            stockSocket = httpService.pushCoinType(code, period, isBibi);
            stockDispo = stockSocket
                    .map(s -> new Gson().fromJson(s, CoinBean.class))
                    .subscribe(coinBean -> LiveDataBus.get().with(isBibi ? RxBusCode.PUSH_COIN_BEAN_BIBI_CHART : RxBusCode.PUSH_COIN_BEAN_LEVEL_CHART, CoinBean.class)
                            .postValue(coinBean), e -> {
                    });
        } else {
            unSub();
            SocketBean socketBean = new SocketBean();
            if (isBibi) {
                socketBean.setType("goods_list");
            } else {
                socketBean.setType("goods_list_lever");
            }
            ArrayList<String> strings = new ArrayList<>();
            strings.add(code);
            socketBean.setList(strings);
            socketBean.setPeriod(period);
            stockSocket.send(new Gson().toJson(socketBean));

        }
        this.code = code;
        this.period = period;
        this.isBibi = isBibi;

    }

    private void unSub() {
        SocketBean socketBean = new SocketBean();
        socketBean.setAction("unsub");
        if (isBibi) {
            socketBean.setType("goods_list");
        } else {
            socketBean.setType("goods_list_lever");
        }
        ArrayList<String> strings = new ArrayList<>();
        strings.add(code);
        socketBean.setList(strings);
        socketBean.setPeriod(period);
        stockSocket.send(new Gson().toJson(socketBean));
    }

    @Override
    public void detachView() {
        DisposUtil.close(stockDispo);
        if (stockSocket != null) {
            stockSocket.close();
            stockSocket = null;
        }
        super.detachView();
    }


}
